﻿//+--------------------------------------------------------------------------------------------------
//     
//Description: View definition localization helpers
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Text;
using VmroleAuthor.ViewModels;

namespace VmroleAuthor.Models
{
    public class ViewDefLocalizationHelper
    {
        public static string LocalizablePropertySyntaxBegin = "{{";
        public static string LocalizablePropertySyntaxEnd = "}}";

        public static string GenerateLocalizableString(string propValue)
        {
            string generated = LocalizablePropertySyntaxBegin + propValue + LocalizablePropertySyntaxEnd;
            return generated;
        }

        /// <summary>
        /// Validates that the input string is parameterized correctly for lcalizationand Parse it for parameter details
        /// </summary>
        /// <param name="inputString"> input string </param>
        /// <param name="parsedLocalizableParam"></param>
        /// <returns>First error message to be displayed. Null if there is no error</returns>
        public static string ValidateAndGetLocalizableParameterName(string inputString, out string parsedLocalizableParam)
        {
            parsedLocalizableParam = null;
            string error;
            if (string.IsNullOrEmpty(inputString))
            {
                return "Cannot be empty";
            }

            if (!inputString.StartsWith(LocalizablePropertySyntaxBegin, StringComparison.Ordinal))
            {
                error = "Invalid localizable property syntax. Localizable Property must start with " + ViewDefLocalizationHelper.LocalizablePropertySyntaxBegin;
                return error;
            }
            if (!inputString.EndsWith(LocalizablePropertySyntaxEnd, StringComparison.Ordinal))
            {
                error = "Invalid localizable property syntax. Localizable Property must end with " + ViewDefLocalizationHelper.LocalizablePropertySyntaxEnd;
                return error;
            }

            // e.g., "Label": "{{ComputerName}}".  Remove 4 for { { } } 
            int localizablePropertyParameterNameLength = inputString.Length - 4;
            if (localizablePropertyParameterNameLength <= 0)
            {
                error = "Invalid localizable property length. Localizable Property length cannot be 0";
                return error;
            }

            parsedLocalizableParam = inputString.Substring(2, localizablePropertyParameterNameLength);

            if (parsedLocalizableParam.Contains(" "))
            {
                error = "Invalid localizable property. Cannot contain white spaces";
                return error;
            }
            return null;        
        }

        public static string ValidatePropertyNotLocalized(string inputString, bool isNotNullable)
        {               
            if (string.IsNullOrEmpty(inputString))
            {
                return isNotNullable ? "Cannot be empty" : null;
            }

            bool paramStartsWith = (inputString.StartsWith(LocalizablePropertySyntaxBegin, StringComparison.Ordinal));
            bool paramEndsWith = inputString.EndsWith(LocalizablePropertySyntaxEnd, StringComparison.Ordinal);
            int localizablePropertyParameterNameLength = inputString.Length - 4;

            if (paramStartsWith && paramEndsWith)
            {
                string error;               
                if (localizablePropertyParameterNameLength > 0)
                {
                    error = inputString.Contains(" ") ? "Localization is de-selected. White spaces not allowed." : "Localizable parameter syntax is valid. Enable localization?";                    
                }
                else
                {
                    error = "Localization is de-selected. Parenthesis valid but parameter name length must be greater than 0";
                }             
                return error;
            }
            return null;
        }

        public static bool IsPropertyLocalized(string inputString)
        {
            string localizableParamName;
            string err = ViewDefLocalizationHelper.ValidateAndGetLocalizableParameterName(inputString, out localizableParamName);
            return string.IsNullOrEmpty(err) && !string.IsNullOrEmpty(localizableParamName);
        }

        public static string ValidatePropertyLocalized(string inputString, ViewDefViewModel viewDefViewModel)
        {
            string localizableParamName;
            string err = ViewDefLocalizationHelper.ValidateAndGetLocalizableParameterName(inputString, out localizableParamName);         
            if (string.IsNullOrEmpty(err))
            {
                StringBuilder sb = new StringBuilder();
                if (string.IsNullOrEmpty(localizableParamName))
                {
                    sb.Append("No localized parameter found. Syntax invalid?");
                }
                else
                {
                    if (viewDefViewModel != null)
                    {
                        if (!viewDefViewModel.ContainsLocalizableParameter(localizableParamName))
                        {
                            sb.Append(" Localizable Parameter not declared in resources: " + localizableParamName);
                        } 
                    }                   
                }
                err = sb.ToString();

            }
            return err;
        }


    }
}
